define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope,Employeemanager_rights,$ionicSlideBoxDelegate,GoPage,GoDialog,$timeout) {
		 //滑动切换
		$scope.selectTab = 0;
		$scope.activeTab = function(index) {
			$scope.selectTab = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide(index);
		};
	    //滑动切换
		$scope.slideChanged = function(index) {
			$scope.selectTab = index;
		};
		 //跳转到视频编辑页面
		$scope.onshow_addrights = function(){
			GoPage.go_states("employeemanager_rights_add");
		}

		$scope.onshow_edit = function(id){
			GoPage.go_states("employeemanager_rights_add",{"id":id});
		}

		//加载数据
		$scope.init_data = function() {
			Employeemanager_rights.part_list(1,function(data) {
				$scope.part_list = data.role;
			});
		}
		$scope.init_data(); //必须在定义之后

		//删除数据
		$scope.deletepart = function(id) {
			Employeemanager_rights.rights_delete(id,function(data) {
				if(data.success){
					var i = 0;
					var n = -1;
					for(var o in $scope.part_list){
						if($scope.part_list[o].id == id){
							n = i;
							break;
						}
						i++;
					}
					if(n != -1){
						$scope.part_list.splice(n,1);
					}
					GoDialog.toast("删除成功！");

				}else{
					GoDialog.toast(data.msg);
				}
			});
			//console.log(id);
		}

		//下拉刷新
        $scope.input = new Object();
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
        $scope.isLoad = true;
        Employeemanager_rights.part_list(1,function(data){
            if(data != null) {
                $scope.part_list = data.role;
            }else{
            	$scope.part_list = new Object();
            }
                $scope.input.nPage = 1;
            },
            function() {
                $scope.$broadcast('scroll.refreshComplete');
                $timeout(function() {
                    $scope.isLoad = false;
                }, 1500);
            });
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            Employeemanager_rights.part_list($scope.input.nPage + 1,function(data) {
                if(data != null) {
                    if($scope.part_list != undefined) {
                        $scope.part_list = $scope.part_list.concat(data.role);
                        $scope.input.nPage += 1;
                    } else {
                        $scope.part_list = data.role;
                    }
                }
            },function() {
                $scope.$broadcast('scroll.infiniteScrollComplete');
                $timeout(function() {
                    $scope.isLoad = false;
                }, 1500);

            });
        };



	}

	controller.$inject = ['$scope', 'Employeemanager_rights','$ionicSlideBoxDelegate','GoPage','GoDialog','$timeout'];

	return app.registerController("employeemanager_rightsctrl",controller);
});